/**
 * localStorage 方法
 */
// 存储数据
export const setItem = (key: string, value: any) => {
  // value分为两种情况
  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  window.localStorage.setItem(key, value);
};
// 获取数据
export const getItem = (key: string) => {
  const data: any = window.localStorage.getItem(key);
  try {
    return JSON.parse(data);
  } catch (error) {
    return data;
  }
};
// 删除指定数据
export const removeItem = (key: string) => {
  window.localStorage.removeItem(key);
};
// 删除所有
export const removeAllItem = () => {
  window.localStorage.clear();
};

/**
 * sessionStorage 方法
 */
 export const sessionSetItem = (key: string, value: any) => {
  // value分为两种情况
  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  window.sessionStorage.setItem(key, value);
};
// 获取数据
export const sessionGetItem = (key: string) => {
  const data: any = window.sessionStorage.getItem(key);
  try {
    return JSON.parse(data);
  } catch (error) {
    return data;
  }
};
// 删除指定数据
export const sessionRemoveItem = (key: string) => {
  window.sessionStorage.removeItem(key);
};
// 删除所有
export const sessionClearAll = () => {
  window.sessionStorage.clear();
};